package laugh.util;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 
*    
* 项目名称：LaughFactory   
* 类名称：PageEncodingFilter   
* 类描述： 编码过滤  
* 创建人：wizard   
* 创建时间：2012-3-27 下午02:49:08   
* 修改人：wizard   
* 修改时间：2012-3-27 下午02:49:08   
* 修改备注：   
* @version 0.9
*
 */
public class PageEncodingFilter implements Filter {

    private String encoding = Constants.ENCODING;

    public void destroy() {
        encoding = null;
    }

    public void doFilter(ServletRequest sreq, ServletResponse sresp,
            FilterChain chain) throws IOException, ServletException {
        sreq.setCharacterEncoding(encoding);
        sresp.setContentType("text/html;charset=" + encoding);
        HttpServletRequest req = (HttpServletRequest) sreq;
        HttpServletResponse res = (HttpServletResponse) sresp;
        chain.doFilter(req, res);
    }

    public void init(FilterConfig fc) throws ServletException {
        encoding = fc.getInitParameter("encoding");
    }
}
